public class Test025 {
    public static void main(String[] args) {
        int[] array={1,2,3,4,5,6,7};
        int index = avg(array,7);
        if(index==-1){
            System.out.println("没有你要找的关键字！");
        }else{
            System.out.println("找到了你要的关键字，下标是："+index);
        }
    }

    public static int avg(int[] array, int key) {
        int left = 0;
        int right = array.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (array[mid] < key) {
                left = mid + 1;
            } else if(array[mid] == key){
                    return mid;
            }else{
                    return mid-1;
                }
            }
        return -1;
        }
    }
